#include <sparrow/types.h>
#include <sparrow/errors.h>

static Octets null_buffer;

Slice::Slice(const Octets &buffer, 
    Octets::const_iterator begin, 
    Octets::const_iterator end)
:   buffer_(&buffer),
    begin_(begin),
    end_(end)
{
}

Slice::Slice()
:   buffer_(&null_buffer),
    begin_(null_buffer.begin()),
    end_(null_buffer.end())
{
}


const Octets &Slice::buffer() const
{
    validate();
    return *buffer_;
}

const Octets::const_iterator &Slice::begin() const
{
    validate();
    return begin_;
}

const Octets::const_iterator &Slice::end() const
{
    validate();
    return end_;
}

void Slice::validate() const
{
    if(buffer_ == &null_buffer)
    {
        throw_error<std::runtime_error>(__FILE__, __LINE__, "access of uninitialized Slice");
    }
}

size_t Slice::size() const
{
    return end() - begin();
}

